import { apiConfig } from './api-config'
import type { 
  Notification
} from '../../../types/notification'

export class NotificationService {
  private baseUrl: string
  private axiosInstance: any

  constructor() {
    // 由于api-config已经配置了全局的/api前缀，这里只需要使用/notification
    this.baseUrl = `/notification`
    // 使用配置好的axios实例，而不是直接使用axios
    this.axiosInstance = apiConfig.getAxiosInstance()
  }

  /**
   * 获取未读通知数量
   */
  async getUnreadCount(): Promise<number> {
    try {
      console.log('开始获取未读通知数量，请求URL:', `${this.baseUrl}/unread/count`)
      const response = await this.axiosInstance.get(`${this.baseUrl}/unread/count`)
      console.log('获取未读通知数量响应:', response)
      if (response.data.code === 0) {
        return response.data.data
      }
      throw new Error(response.data.message || '获取未读通知数量失败')
    } catch (error) {
      console.error('获取未读通知数量失败:', error)
      throw error
    }
  }

  /**
   * 获取通知列表
   */
  async getNotifications(page = 1, size = 10): Promise<{ list: Notification[], total: number }> {
    try {
      const response = await this.axiosInstance.get(`${this.baseUrl}/list`, {
        params: { page, size }
      })
      if (response.data.code === 0) {
        // 后端返回的是IPage<Notification>结构，包含records和total
        return {
          list: response.data.data.records || [],
          total: response.data.data.total || 0
        }
      }
      throw new Error(response.data.message || '获取通知列表失败')
    } catch (error) {
      console.error('获取通知列表失败:', error)
      throw error
    }
  }

  /**
   * 获取通知详情
   */
  async getNotification(id: number): Promise<Notification> {
    try {
      const response = await this.axiosInstance.get(`${this.baseUrl}/${id}`)
      if (response.data.code === 0) {
        return response.data.data
      }
      throw new Error(response.data.message || '获取通知详情失败')
    } catch (error) {
      console.error('获取通知详情失败:', error)
      throw error
    }
  }

  /**
   * 标记通知为已读
   */
  async markAsRead(id: number): Promise<void> {
    try {
      // 后端API是POST /read，接收ID列表
      const response = await this.axiosInstance.post(`${this.baseUrl}/read`, [id])
      if (response.data.code !== 0) {
        throw new Error(response.data.message || '标记通知为已读失败')
      }
    } catch (error) {
      console.error('标记通知为已读失败:', error)
      throw error
    }
  }

  /**
   * 标记所有通知为已读
   */
  async markAllAsRead(): Promise<void> {
    try {
      // 后端API是POST /read/all
      const response = await this.axiosInstance.post(`${this.baseUrl}/read/all`)
      if (response.data.code !== 0) {
        throw new Error(response.data.message || '标记所有通知为已读失败')
      }
    } catch (error) {
      console.error('标记所有通知为已读失败:', error)
      throw error
    }
  }

  /**
   * 删除通知
   */
  async deleteNotifications(ids: number[]): Promise<void> {
    try {
      // 后端API期望直接是ID列表，不是包装在对象中
      const response = await this.axiosInstance.delete(`${this.baseUrl}/delete`, {
        data: ids
      })
      if (response.data.code !== 0) {
        throw new Error(response.data.message || '删除通知失败')
      }
    } catch (error) {
      console.error('删除通知失败:', error)
      throw error
    }
  }
}

// 创建全局通知服务实例
export const notificationService = new NotificationService()